package com.massimilla.test;

import java.util.*;

public class NumberGame {

	public static void main(String[] args) {
		// Creates random integer
		boolean play = true;
		do{
			Random generator = new Random();
			System.out.println("Let's play a number guessing game!");
			System.out.println("Type the number you would like to guess up to.");
			Scanner scanrange = new Scanner(System.in);
			int range = scanrange.nextInt();
			int num = generator.nextInt(range);
			int tries = range / 20;
			if(tries <= 2){
				tries = 3;
			}
			System.out.println("You have " + tries + " tries." );
			System.out.println("Make a guess!");

			// Gives clues and allows retries
			boolean game = true;
			int i = 0;
			while(game == true){
				i++;
				int retry = tries - i;
				int guess = scanrange.nextInt();
				// Conditions for a wrong guess that does not result in losing the game
				if(guess > num){
					System.out.println("My number is less than your guess");
					if(retry > 1){
						System.out.println("You now have " + retry + " tries." );
					}
					if(retry == 1){
						System.out.println("You now have " + retry + " try." );
					}
				}
				else if(guess < num){
					System.out.println("My number is less than your guess");
					if(retry > 1){
						System.out.println("You now have " + retry + " tries." );
					}
					if(retry == 1){
						System.out.println("You now have " + retry + " try." );
					}
				}
				// Conditions for winning and replaying
				else if(guess == num){
					System.out.println ("You guessed my number!");
					System.out.println ("Congratulations! You win!");
					System.out.println ("Would you like to play again?");
					String again = scanrange.next();
					if(again.equalsIgnoreCase("no") || again.equalsIgnoreCase("nah") || again.equalsIgnoreCase("na")){
						System.out.println ("Goodbye!");
						play = false;
						game = false;
					}
					else{
						play = true;
						game = false;
					}
				}
				// Conditions for losing
				if(i == tries){
					System.out.println("Too bad.");
					System.out.println("You didn't guess my number");
					System.out.println("My number was " + num);
					System.out.println ("Would you like to play again?");
					String again = scanrange.next();
					if(again.equalsIgnoreCase("no") || again.equalsIgnoreCase("nah") || again.equalsIgnoreCase("na")){
						System.out.println ("Goodbye!");
						play = false;
						game = false;
					}
					else{
						play = true;
						game = false;
					}				
				} 
				if(i == tries){
					game = false;				
				}
			} scanrange.close();			
		}
		while(play == true);
	}
}

